<?php

namespace app\common\service\product\factory;

use app\common\enum\product\DeductStockTypeEnum;
use app\common\model\plus\discount\DiscountSku as ProductSkuModel;
use app\common\model\plus\discount\Product as ProductModel;
use think\facade\Cache;

/**
 * 商品来源-普通商品扩展类
 */
class DiscountProductService extends ProductService
{
    /**
     * 更新商品库存 (针对下单减库存的商品)
     */
    public function updateProductStock($productList, $commomOrder = [])
    {
        foreach ($productList as $product) {
            $conn = new \think\cache\driver\Redis(config('cache.stores.redis'));
            $discount_stock = $conn->get($product['sku_source_id'] . '_discount_stock');
            $discount_product_id = $conn->get($product['sku_source_id'] . '_discount_product_id');
            if (empty($discount_stock)) {
                // 下单减库存
                $sku = ProductSkuModel::detailLock($product['sku_source_id']);
                $discount_stock = $sku['discount_stock'];
                $conn->set($product['sku_source_id'] . '_discount_stock', $discount_stock);
                $conn->set($product['sku_source_id'] . '_discount_product_id', $sku['discount_product_id']);
            }

            if (empty($discount_product_id)) {
                // 下单减总库存
                $sku = ProductSkuModel::detailLock($product['sku_source_id']);
                $spro = (new ProductModel)->where('discount_product_id', '=', $sku['discount_product_id'])->lock(true)->find();
                $product_stock = $spro['stock'];
                $discount_product_id = $sku['discount_product_id'];
                $conn->set($product['sku_source_id'] . '_discount_product_id', $discount_product_id);
                $conn->set($sku['discount_product_id'] . '_product_stock', $product_stock);
            }
            $product_stock = $conn->get($discount_product_id . '_product_stock');
//            (new ProductModel)->where('discount_product_id', '=', $sku['discount_product_id'])->inc('join_num')->update();
            (new ProductModel)->where('discount_product_id', '=', $discount_product_id)->inc('join_num')->update();
//            if ($product['deduct_stock_type'] == DeductStockTypeEnum::CREATE) {
//            if ($sku['discount_stock'] >= $product['total_num'] && $spro['stock'] >= $product['total_num']) {
            if ($discount_stock >= $product['total_num'] && $product_stock >= $product['total_num']) {
                try {
                    $conn->watch($product['sku_source_id'] . '_discount_stock');
                    $conn->watch($discount_product_id . '_product_stock');
                    $conn->multi();
                    $conn->set($product['sku_source_id'] . '_discount_stock', $discount_stock - $product['total_num']);
                    $conn->set($discount_product_id . '_product_stock', $product_stock - $product['total_num']);
                    $tag = $conn->exec();
                    if ($tag) {
                        // 主库存减少
//                        (new ProductModel)->where('discount_product_id', '=', $sku['discount_product_id'])->dec('stock', $product['total_num'])->update();
                        (new ProductModel)->where('discount_product_id', '=', $discount_product_id)->dec('stock', $product['total_num'])->update();
                        // 下单减库存
//                        (new ProductSkuModel)->where('discount_product_sku_id', '=', $sku['discount_product_sku_id'])->dec('discount_stock', $product['total_num'])->update();
                        (new ProductSkuModel)->where('discount_product_sku_id', '=', $product['sku_source_id'])->dec('discount_stock', $product['total_num'])->update();
                    } else {
                        return false;
                    }
                } catch (\Exception $e) {
                    log_write('discount updateProductStock' . $e->getMessage());
                }
                return true;
            }
            return false;
//            }
        }
    }

    public function updateStockSales($productList)
    {
        foreach ($productList as $product) {
            $sku = ProductSkuModel::detail($product['sku_source_id']);
            // 记录商品的销量
            (new ProductModel)->where('discount_product_id', '=', $sku['discount_product_id'])->inc('total_sales', $product['total_num'])->update();
            // 付款减库存
//            if ($product['deduct_stock_type'] == DeductStockTypeEnum::PAYMENT) {
//                try {
//                    // 主库存减少
//                    (new ProductModel)->where('discount_product_id', '=', $sku['discount_product_id'])->dec('stock', $product['total_num'])->update();
//                    // 下单减库存
//                    (new ProductSkuModel)->where('discount_product_sku_id', '=', $sku['discount_product_sku_id'])->dec('discount_stock', $product['total_num'])->update();
//                } catch (\Exception $e) {
//                    log_write('discount updateStockSales' . $e->getMessage());
//                }
//            }
        }
        return true;
    }


    public function backProductStock($productList, $isPayOrder = false)
    {
        foreach ($productList as $product) {
            // 1,未付款订单并且创建时减库存，回退库存 2,已付款订单回退
//            if ((!$isPayOrder && $product['deduct_stock_type'] == DeductStockTypeEnum::CREATE)
//                || $isPayOrder
//            ) {
            if (!$isPayOrder || $isPayOrder
            ) {
                $sku = ProductSkuModel::detail($product['sku_source_id']);
                // 回退主库存
                (new ProductModel)->where('discount_product_id', '=', $sku['discount_product_id'])->inc('stock', $product['total_num'])->update();
                // 回退sku库存
                (new ProductSkuModel)->where('discount_product_sku_id', '=', $sku['discount_product_sku_id'])->inc('discount_stock', $product['total_num'])->update();
            }
        }
    }

}
